package com.test;

import org.checkerframework.checker.nullness.compatqual.NullableDecl;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.downloader.Downloader;
import us.codecraft.webmagic.downloader.selenium.SeleniumDownloader;
import us.codecraft.webmagic.example.GithubRepoPageMapper;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.processor.example.GithubRepoPageProcessor;

import java.util.concurrent.TimeUnit;

public class CynenProcessor implements PageProcessor {
    private static String initUrl="http://www.p42u.com/";
    WebDriver driver = null;
    public CynenProcessor() {
        // WebDriver driver = new FirefoxDriver();
        // Chrome浏览器.需要下载指定的固定driver.
        System.setProperty("webdriver.chrome.driver","D:/develop/driver/chromedriver/chromedriver.exe");
        this.driver = new ChromeDriver();
    }


    public static void main(String[] args) {
        //selenium系统配置，其中的路径写自己config文件的路径
        System.setProperty("selenuim_config", "D:\\develop\\Git\\GitStore\\crawler\\selenium-demo\\src\\main\\resources\\config.properties");

        /*// 显示的等待,必须等待到关注的元素出现才退出
        WebElement element = (new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>() {
            @NullableDecl
            public WebElement apply(@NullableDecl WebDriver webDriver) {
                return webDriver.findElement(By.linkText("讲师"));
            }
        });
        System.out.println(element.getAttribute("href"));
        element.click();*/
        Downloader downloader = new SeleniumDownloader("D:/develop/driver/chromedriver/chromedriver.exe");
        Spider.create(new CynenProcessor())
                .addUrl(initUrl)
                .setDownloader(downloader)
                // .thread(1)
                .run();


    }

    public void process(Page page) {
        int length = page.getUrl().toString().length();
        if (length == 20){
            initPage(page);
        }else {
            page.getHtml();
            System.out.println("-----------");
        }


    }
    // 初始化页面,到列表选项页.
    private void initPage(Page page) {
        // FireFox浏览器.
        // System.setProperty("webdriver.gecko.driver","C:/Program Files (x86)/Mozilla Firefox/firefox.exe");
        // WebDriver driver = new FirefoxDriver();
        // Chrome浏览器.需要下载指定的固定driver.
        // System.setProperty("webdriver.chrome.driver","D:/develop/driver/chromedriver/chromedriver.exe");
        // WebDriver driver = new ChromeDriver();
        long start = System.currentTimeMillis();
        // driver.get(initUrl);
        // 隐式的等待,页面没有加载完成,就等待这个时间,停止加载.
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        try {
            Thread.sleep(10000);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        // 1.page1
        WebElement webElement = ((ChromeDriver) driver).findElementByLinkText("简体中文");
        System.out.println(webElement.getAttribute("href"));
        webElement.click();
        long end = System.currentTimeMillis();
        System.out.println("点击 == 简体中文 == 耗时" + (end - start)+" ms" );

        try {
            // page2
            WebElement id = ((ChromeDriver) driver).findElementById("adultwarningprompt");
            WebElement elementByCssSelector = ((ChromeDriver) driver).findElementByCssSelector("div#adultwarningprompt input.btnAdultAgree");
            elementByCssSelector.click();
        }catch (NoSuchElementException e){
            System.out.println("没有弹出确认框...");
        }

        // 进入了主页面 page3
        WebElement categoryLink = ((ChromeDriver) driver).findElementByLinkText("依分类");
        System.out.println(categoryLink.getAttribute("href"));
        categoryLink.click();
    }

    private Site site = Site.me()
            .setTimeOut(30000)
            .setCharset("utf8")
            .setRetryTimes(3000)
            .setSleepTime(1000);

    public Site getSite() {
        return site;
    }
}
